import requests
from fake_useragent import UserAgent

# 请求方法名称与http方法名称相同
url = "http://127.0.0.1:8000"
# username = input("请输入用户名：")
# password = input("请输入密码：")


# payload = {
#     "username":username,
#     "password":password
# }


# get请求带参数
# response = requests.get(url,params=payload)


# post请求带参数
# response = requests.post(url,data=payload)

# 添加请求头，【伪造浏览器请求头】，每次请求不同
ua = UserAgent(verify_ssl=False)
headers = {
    "User-Agent": ua.random
}

response = requests.get(url,headers=headers)


# print(response)
# print(dir(response))
# print(f'status_code:{response.status_code}')
# print(f'headers:{response.headers}')

# 根据返回类型的content-type:text/html;charset=utf-8 判断
# print(f'content:{response.content}') # byte类型
# print(f'text:{response.text}') # 获取到文本
print(f'json:{response.json()}') # 获取到文本


# 下载图片至本地
# pic_url = "https://www.baidu.com/img/baidu_resultlogo@2.png"
# try:
#     response = requests.get(pic_url)
# except:
#     print("请求失败")
# else:
#     print(response.content)
#     with open('pic.png','wb') as f:
#         f.write(response.content)
